package com.wang.project.file;


import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.wang.project.common.BaseResponse;
import com.wang.project.common.ErrorCode;
import com.wang.project.common.ResultUtils;
import com.wang.project.file.factory.FileEventContainer;
import com.wang.project.file.factory.FileEventService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 文件事件入口
 */
@Slf4j
@RestController
@RequestMapping("/file/fileEvent")
public class FileEventController {

    @PostMapping(value = "/test")
    public BaseResponse<Object> test() {
//        List<FileUserAvatarUrlVO> fileUserAvatarUrlVOLis = new ArrayList<>();
//        fileUserAvatarUrlVOLis.add(new FileUserAvatarUrlVO("test1", "test1Vlaue"));
//        fileUserAvatarUrlVOLis.add(new FileUserAvatarUrlVO("test2", "test2Vlaue"));
//        fileUserAvatarUrlVOLis.add(new FileUserAvatarUrlVO("test3", "test3Vlaue"));
//        fileUserAvatarUrlVOLis.add(new FileUserAvatarUrlVO("test4", "test4Vlaue"));
//        return ResultUtils.success(new FileUserAvatarUrlVO("test1", "test1Vlaue"));
        return ResultUtils.success(false);
    }


    /**
     * @param fileEventDTO
     * @return
     */
    @PostMapping(value = "/entrance", produces = "application/json;charset=UTF-8")
    public BaseResponse<Object> entrance(@RequestBody FileEventDTO fileEventDTO) {
        if (fileEventDTO == null) {
            log.info("FileController entranceToVoid fileEventDTO is null");
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        }
        log.info("FileEventController entranceToVoid:{}", JSONUtil.toJsonStr(fileEventDTO));
        FileEventService service = FileEventContainer.get(fileEventDTO.getEvent());
        try {
            /**
             * 返回值直接用Object
             * 入参的数据使用Object类型，后续通过工具类进行转换
             */
            return (BaseResponse<Object>) service
                    .getClass()
                    .getMethod(fileEventDTO.getFuncName(), String.class)
                    .invoke(service, JSON.toJSONString(fileEventDTO.getData()));
        } catch (Exception e) {
            log.info("FileController entrance exceptionL:{}", e);
        }
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR);
    }
}
